import 'dart:io';

import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:flutter_frontend/pages/mine/upgrade_model.dart';
import 'package:flutter_frontend/utils/assets_path.dart';
import 'package:flutter_frontend/utils/my_navigator_route.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:r_upgrade/r_upgrade.dart';

class MyDialog {
  MyDialog._();

  /// 警告弹窗
  /// title 标题
  /// content 内容
  /// cancelText 取消文字
  /// confirmText 确定文字
  static Future<bool?> showWaringDialog(BuildContext context, {String? title, required String content, String? cancelText, String? confirmText}) async {
    return await showDialog(
      context: context,
      builder: (BuildContext ctx) {
        return SimpleDialog(
            contentPadding: const EdgeInsets.only(top: 80, left: 60, bottom: 60, right: 60).w,
            backgroundColor: Theme.of(context).colorScheme.background,
            shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(16).w),
            children: [
              if (title != null)
                Padding(
                  padding: const EdgeInsets.only(bottom: 48).w,
                  child: Text(title, textAlign: TextAlign.center, style: Theme.of(context).textTheme.labelLarge?.copyWith(fontWeight: FontWeight.w700)),
                ),
              Text(content, textAlign: TextAlign.center, style: Theme.of(context).textTheme.labelMedium),
              SizedBox(height: 60.w),
              Row(children: [
                Expanded(
                  child: InkWell(
                    child: Container(
                      alignment: Alignment.center,
                      height: 80.w,
                      decoration: BoxDecoration(
                        color: Theme.of(context).colorScheme.background,
                        borderRadius: BorderRadius.all(Radius.circular(10.w)),
                        border: Border.all(color: Theme.of(context).textTheme.labelSmall!.color!),
                      ),
                      child: Text(cancelText ?? '取消', style: Theme.of(context).textTheme.labelSmall?.copyWith(fontSize: 36.sp), textAlign: TextAlign.center),
                    ),
                    onTap: () => MyNavigatorRoute.pop(ctx, false),
                  ),
                ),
                SizedBox(width: 30.w),
                Expanded(
                  child: InkWell(
                    child: Container(
                      alignment: Alignment.center,
                      height: 80.w,
                      decoration: BoxDecoration(color: Theme.of(context).colorScheme.primary, borderRadius: BorderRadius.all(Radius.circular(10.w))),
                      child: Text(confirmText ?? '确定', style: TextStyle(color: Colors.white, fontSize: 36.sp), textAlign: TextAlign.center),
                    ),
                    onTap: () => MyNavigatorRoute.pop(ctx, true),
                  ),
                ),
              ])
            ]);
      },
    );
  }

  /// 警告弹窗
  /// title 标题
  /// content 内容
  /// confirmText 确定文字
  static Future<bool?> showRemindDialog(BuildContext context, {String? title, required String content, String? confirmText}) async {
    return await showDialog(
      context: context,
      builder: (BuildContext ctx) {
        return SimpleDialog(
            contentPadding: const EdgeInsets.only(top: 60, left: 30, bottom: 35, right: 30).w,
            backgroundColor: Theme.of(context).colorScheme.background,
            shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(16).w),
            children: [
              if (title != null)
                Padding(
                  padding: const EdgeInsets.only(bottom: 24).w,
                  child: Text(
                    title,
                    textAlign: TextAlign.center,
                    style: Theme.of(context).textTheme.labelLarge?.copyWith(fontWeight: FontWeight.w700),
                  ),
                ),
              Text(content, textAlign: TextAlign.center, style: Theme.of(context).textTheme.labelMedium),
              SizedBox(height: 44.w),
              InkWell(
                child: Container(
                  alignment: Alignment.center,
                  height: 80.w,
                  decoration: BoxDecoration(color: Theme.of(context).colorScheme.primary, borderRadius: BorderRadius.all(Radius.circular(10.w))),
                  child: Text(confirmText ?? '确定', style: const TextStyle(color: Colors.white), textAlign: TextAlign.center),
                ),
                onTap: () => MyNavigatorRoute.pop(ctx, true),
              ),
            ]);
      },
    );
  }

  /// 更新弹窗
  static void showUpgradeDialog(BuildContext context, UpgradeModel upgradeModel) {
    showDialog(
      context: context,
      builder: (BuildContext ctx) {
        return SimpleDialog(backgroundColor: Colors.transparent, children: [
          Container(
            decoration: BoxDecoration(
              color: Theme.of(context).colorScheme.background,
              borderRadius: const BorderRadius.all(Radius.circular(16)).w,
            ),
            child: Column(children: [
              Padding(
                padding: const EdgeInsets.symmetric(vertical: 70).w,
                child: Image.asset(AssetsPath.imagePath('icon_logo', directory: 'logo'), width: 148.w, height: 161.w),
              ),
              Text('版本更新', textAlign: TextAlign.center, style: Theme.of(context).textTheme.labelLarge?.copyWith(fontSize: 40.sp)),
              Padding(
                padding: const EdgeInsets.only(top: 30, bottom: 60).w,
                child: Text('发现新的版本，是否更新？', textAlign: TextAlign.center, style: Theme.of(context).textTheme.labelMedium),
              ),
              InkWell(
                child: Container(
                  alignment: Alignment.center,
                  margin: const EdgeInsets.only(left: 32, bottom: 32, right: 32).w,
                  height: 80.w,
                  decoration: BoxDecoration(color: Theme.of(context).colorScheme.primary, borderRadius: BorderRadius.all(Radius.circular(10.w))),
                  child: Text('下载并安装', style: TextStyle(color: Colors.white, fontSize: 32.sp), textAlign: TextAlign.center),
                ),
                onTap: () async {
                  final String? apkUrl = upgradeModel.apkUrl;
                  final String? apkName = upgradeModel.apkName;
                  MyNavigatorRoute.pop(ctx);
                  if (apkUrl == null || apkUrl.isEmpty) {
                    EasyLoading.showToast('未配置下载地址');
                    return;
                  }
                  RUpgrade.setDebug(kDebugMode);
                  if (Platform.isIOS) RUpgrade.upgradeFromAppStore('appId');
                  if (Platform.isAndroid) {
                    final int? lastUpgradedId = await RUpgrade.getLastUpgradedId();
                    if (lastUpgradedId == null) {
                      RUpgrade.upgrade(apkUrl, fileName: apkName, notificationStyle: NotificationStyle.speechAndPlanTime);
                      return;
                    }
                    RUpgrade.upgradeWithId(lastUpgradedId);
                  }
                },
              ),
            ]),
          ),
          SizedBox(height: 90.w),
          InkWell(
            child: Image.asset(AssetsPath.imagePath('icon_close_round'), width: 72.w, height: 72.w),
            onTap: () => MyNavigatorRoute.pop(ctx),
          ),
        ]);
      },
    );
  }
}
